import os
import sys
from pathlib import Path
from functools import lru_cache

__all__ = ["Resource_Path"]

@lru_cache(maxsize=None)
def Resource_Path(relative_path: str) -> str:
    # 如果传进来的已经是绝对路径，直接返回
    if os.path.isabs(relative_path):
        return os.path.normpath(relative_path)
        print(f"1.绝对路径{os.path.normpath(relative_path)}")

    # PyInstaller onefile 运行时会被解压到临时目录，_MEIPASS 指向该目录
    if hasattr(sys, "_MEIPASS"):
        base = Path(sys._MEIPASS).parent
        print(f"2.PyInstaller onefile路径{base}")
    else:
        # 普通 Python
        base = Path(__file__).resolve().parent
        print(f"3.Python路径{base}")
        if base.name.endswith((".dist", ".build", "_internal")):
        # Nuitka 资源在同级目录或 _internal 目录，这里统一向上跳一层
            base = base.parent
            print(f"4.Nuitka standalone路径{base}")
    return str((base / relative_path).resolve())